using System;
using Glade;
using Gtk;

namespace Parking {
	public class DlgReprendre {
		public DlgReprendre (Borne pBorne) {
			Glade.XML gxml = new Glade.XML (null, "Parking.reprendre.glade", "dlgReprendre", null);
			gxml.Autoconnect (this);
			borne=pBorne;
		}
		
		public Borne borne { get; private set; }
		
		[Glade.Widget]
		Entry num_ticket;
		
		[Glade.Widget]
		Dialog dlgReprendre;
		
		public void on_annuler_clicked (System.Object obj, EventArgs e) {
			dlgReprendre.Destroy();
		}
		
		public void on_valider_clicked (System.Object obj, EventArgs e) {
			int numero_ticket = 0;
			try {
				numero_ticket = Int32.Parse(num_ticket.Text);
			} catch (ArgumentNullException) {
				return;
			} catch (FormatException) {
				return;
			}
			
			
			Ticket t = borne.reprendreVoiture(numero_ticket);
			Dialog dialog = null;
			try {
    			dialog = new Dialog ("Recuperer",null,	DialogFlags.DestroyWithParent | DialogFlags.Modal,"Ok", ResponseType.Ok);
			if (t == null) {
				dialog.VBox.Add (new Label ("Votre ticket est invalide !!!"));
			} else {
				dlgReprendre.Destroy();
					
				TimeSpan d = t.duree_stationnement;
				String jours = "";
				String heures = "";
				String minutes = "";
				String secondes = "";
				
				if (d.Days > 0)
				       jours += d.Days + (d.Days > 1 ? " jours " : " jour ");
				
				if (d.Hours > 0)
				       heures += d.Hours + (d.Hours > 1 ? " heures " : " heure ");
				
				if (d.Minutes > 0)
				       minutes += d.Minutes + (d.Minutes > 1 ? " minutes " : " minute ");
				
				if (d.Seconds > 0)
				       secondes += d.Seconds + (d.Seconds > 1 ? " secondes " : " seconde ");
				
				String res = (jours + heures + minutes + secondes).Trim();
					
				dialog.VBox.Add (new Label ("Numéro du ticket : " + t.numero + "\nVous êtes resté : " + res));
			}
				
				dialog.ShowAll ();
    			dialog.Run ();
				
		} finally {
    			if (dialog != null)
        			dialog.Destroy ();
		}
	  }
   }
}